from PyQt5.QtWidgets import (QApplication, QMainWindow, QLabel, QWidget,
                             QVBoxLayout, QPushButton, QHBoxLayout, QComboBox, QDesktopWidget)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
import sys

from win32com.client.gencache import versionRedirectMap

# 修改导入语句
from UI.command import CommandUI
from UI.home import HomeUI
from UI.model_catalogue import ModelPageCatalogue
from UI.version import VersionPage
from UI.model import ModelPage
from UI.option import OptionPage
from UI.clear import ClearUI
from UI.hardware import HardwareUI  # 添加这行导入
from UI.merge_pdf import PdfUI
from UI.publishing import PublishingUI


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("自动处理工具")
        
        # 获取显示器尺寸并居中窗口
        screen = QDesktopWidget().availableGeometry()
        window_width = 900
        window_height = 700
        x = (screen.width() - window_width) // 2
        y = (screen.height() - window_height) // 2
        self.setGeometry(x, y, window_width, window_height)
        
        self.setWindowIcon(QIcon('./img/icon.ico'))

        # 创建主窗口部件和布局
        main_widget = QWidget()
        self.setCentralWidget(main_widget)
        main_layout = QVBoxLayout(main_widget)
        main_layout.setContentsMargins(0, 0, 0, 0)

        # 创建顶部工具栏
        top_bar = QWidget()
        top_bar.setFixedHeight(40)
        top_bar.setStyleSheet("background-color: #DCDCDC; border-bottom: 1px solid #A9A9A9;")
        top_layout = QHBoxLayout(top_bar)
        top_layout.setContentsMargins(10, 0, 10, 0)

        # 创建语言选择下拉框
        self.lang_combo = QComboBox()
        self.lang_combo.addItems(['中文', '英文'])
        self.lang_combo.setFixedWidth(80)
        self.lang_combo.setStyleSheet("""
            QComboBox {
                border: 1px solid #DCDCDC;
                border-radius: 3px;
                padding: 3px;                     
                background: #DCDCDC;
                font-size: 14px;
                font-family: "Microsoft YaHei";
            }
            QComboBox QAbstractItemView {
                font-size: 14px;
                font-family: "Microsoft YaHei";
            }
        """)

        # 将下拉框添加到顶部工具栏右侧
        top_layout.addStretch()
        top_layout.addWidget(self.lang_combo)

        # 创建内容区布局
        content_widget = QWidget()
        content_widget.setObjectName("content_widget")  # 添加这行
        content_layout = QHBoxLayout(content_widget)
        content_layout.setContentsMargins(0, 0, 0, 0)

        # 创建左侧边栏
        sidebar = QWidget()
        sidebar.setFixedWidth(170)  # 将宽度从原来的值改为150像素
        sidebar.setStyleSheet("""
            QWidget {
                background-color: #DCDCDC;
                border-right: 2px solid #A9A9A9;
            }
            QPushButton {
                border: none;
                padding: 10px;
                text-align: left;
                color: #657ce2;
                font-size: 16px;
                font-family: "Microsoft YaHei";
            }
            QPushButton:hover {
                background-color: #C0C0C0;
            }
            QPushButton:pressed {
                background-color: #C0C0C0;
            }
        """)

        # 设置内容区样式
        self.content = QLabel()
        self.content.setAlignment(Qt.AlignCenter)
        self.content.setStyleSheet("""
            QLabel {
                color: #2c3e50;
                font-size: 24px;
                font-family: "Microsoft YaHei";
                background-color: #DCDCDC;
            }
        """)

        # 设置主窗口样式
        main_widget.setStyleSheet("""
            QWidget {
                background-color: #DCDCDC;
            }
        """)

        sidebar_layout = QVBoxLayout(sidebar)

        # 创建侧边栏按钮
        buttons = ['首页', '分册编号', '模块编号', 'PDF合稿','PDF一键合并','11x命令表格检查', '硬件手册', '清稿', '版本信息',
                   '操作指南']  # 添加硬件手册
        self.pages = {}
        self.buttons = {}  # 存储按钮引用

        for text in buttons:
            button = QPushButton(text)
            button.clicked.connect(lambda checked, name=text: self.switch_page(name))
            sidebar_layout.addWidget(button)
            self.buttons[text] = button  # 保存按钮引用

        sidebar_layout.addStretch()

        # 将侧边栏和内容区添加到内容布局
        content_layout.addWidget(sidebar)
        content_layout.addWidget(self.content)

        # 将顶部工具栏和内容区添加到主布局
        main_layout.addWidget(top_bar)
        main_layout.addWidget(content_widget)

        # 预先创建所有页面实例并设置父窗口
        content_widget = self.findChild(QWidget, "content_widget")
        self.pages = {
            '首页': HomeUI(content_widget),
            '版本信息': VersionPage(content_widget),
            '分册编号': ModelPage(content_widget),
            '模块编号': ModelPageCatalogue(content_widget),
            'PDF合稿': PdfUI(content_widget),
            'PDF一键合并': PublishingUI(content_widget),
            '操作指南': OptionPage(content_widget),
            '清稿': ClearUI(content_widget),
            '硬件手册': HardwareUI(content_widget),  # 添加硬件手册页面
            '11x命令表格检查': CommandUI(content_widget)
        }

        # 连接首页按钮信号
        self.pages['首页'].switch_page.connect(self.handle_home_page_switch)

        # 初始隐藏所有页面并显示首页
        for page in self.pages.values():
            page.hide()

        # 显示首页并设置首页按钮样式
        self.content = self.pages['首页']
        content_layout.addWidget(self.content)
        self.content.show()
        self.buttons['首页'].setStyleSheet("background-color: #C0C0C0;")

    def switch_page(self, page_name):
        # 重置所有按钮样式
        for button in self.buttons.values():
            button.setStyleSheet("")
        # 设置当前选中按钮的样式
        self.buttons[page_name].setStyleSheet("""
            background-color: #C0C0C0;
        """)

        # 获取内容区布局
        content_widget = self.findChild(QWidget, "content_widget")
        content_layout = content_widget.layout()

        # 隐藏当前页面
        if self.content:
            self.content.hide()
            content_layout.removeWidget(self.content)

        # 显示新页面
        self.content = self.pages.get(page_name)
        if self.content:
            content_layout.addWidget(self.content)
            self.content.show()
        else:
            # 创建默认标签页面
            self.content = QLabel(page_name, content_widget)
            self.content.setAlignment(Qt.AlignCenter)
            self.content.setStyleSheet("""
                QLabel {
                    color: #2c3e50;
                    font-size: 24px;
                    font-family: "Microsoft YaHei";
                    background-color: #DCDCDC;
                }
            """)
            content_layout.addWidget(self.content)

    def handle_home_page_switch(self, page_name):
        # 将页面标识转换为对应的按钮文本
        page_map = {
            'volume': '分册编号',
            'module': '模块编号',
            'pdf':'PDF合稿',
            'pdf_publishing':'PDF一键合并',
            'clear': '清稿',
            'version': '版本信息',
            'option': '操作指南',
            'hardware': '硬件手册',
            'command': '11x命令表格检查'  # 添加硬件手册映射
        }
        if page_name in page_map:
            self.switch_page(page_map[page_name])


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
